package com.lh.runmanage.controller;

import com.lh.runmanage.dto.ProjAvgDto;
import com.lh.runmanage.model.InPutParam.ProjAvgAllInParam;
import com.lh.runmanage.model.InPutParam.SystemUserInParam;
import com.lh.runmanage.model.InPutParam.SystemUserP2InParam;
import com.lh.runmanage.model.ResultVO;
import com.lh.runmanage.model.UserCountTotalModel;
import com.lh.runmanage.service.ProjAvgService;
import com.lh.runmanage.service.ProjectAndProductService;
import com.lh.runmanage.service.SystemUserService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.LhClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-23 15:58
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/systemUserController")
@Api(value = "项目控制层", description = "系统运行态势平台展示端相关方法")
public class SystemUserController {
    @Autowired
    SystemUserService systemUserService;
    @Autowired
    GetPropertiesClass getPropertiesClass;
    @Autowired
    ProjectAndProductService projectAndProductService;
    @Autowired
    ProjAvgService projAvgService;

    /**
     * 展示端方法：U3，方法ID：SE20210723154601652276E9A16630C
     *
     * @param projectId  表report_systemUser,字段名projectCode:项目CODE
     * @param createDate 表report_systemUser,字段名createDate:所属年月
     * @return 得到项目信息与标定、分值信息
     */
    @ApiOperation(value = "展示端方法：U3，[得到项目信息与标定、分值信息],方法ID：SE20210723154601652276E9A16630C", notes = "/systemUserController/V1/selectSystemUserByProjectIdOrCreateData?projectId=projectId&createDate=createDate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataType = "String")
            , @ApiImplicitParam(name = "createDate", value = "所属年月", dataType = "Date")
    })
    @PostMapping("/V1/selectSystemUserByProjectIdOrCreateData")
    public ResultVO selectSystemUserByProjectIdOrCreateData(@RequestParam(value = "projectId", required = false) String projectId
            , @RequestParam(value = "createDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createDate) throws UnsupportedEncodingException {
        return selectSystemUserMaxByProjectId(projectId,createDate,false);
    }

    /**
     * 展示端方法：J5、J3，方法ID：SE20210723154601652276E9A16630C
     *
     * @param projectId  表report_systemUser,字段名projectCode:项目CODE
     * @param createDate 表report_systemUser,字段名createDate:所属年月
     * @return 得到项目信息与标定、分值信息
     */
    @ApiOperation(value = "展示端方法：J5、J3，[得到项目信息与标定、分值信息],方法ID：SE20210723154601652276E9A16630D", notes = "/systemUserController/V1/selectSystemUserByProjectIdOrCreateData?projectId=projectId&createDate=createDate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataType = "String")
            , @ApiImplicitParam(name = "createDate", value = "所属年月", dataType = "Date")
    })
    @PostMapping("/V1/selectSystemUserMaxByProjectIdOrCreateData")
    public ResultVO selectSystemUserMaxByProjectIdOrCreateData(@RequestParam(value = "projectId", required = false) String projectId
            , @RequestParam(value = "createDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date createDate) throws UnsupportedEncodingException {
        return selectSystemUserMaxByProjectId(projectId, createDate, false);
    }

    private ResultVO selectSystemUserMaxByProjectId(String projectId
            , Date createDate, boolean colSign) throws UnsupportedEncodingException {
        if (projectId != null) {
            projectId = projectId.trim();
            if (projectId.getBytes("gbk").length > 255) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：255", "projectId"), ResultVO.class, null);
            }
        }

        if (createDate == null) {
            createDate = LhClass.getDefaultDate();
        }

        SystemUserInParam systemUserInParam = new SystemUserInParam();
        systemUserInParam.setProjectCode(projectId);
        systemUserInParam.setCreateDate(createDate);

        int nowCounter = systemUserService.selectSystemUserByProjectIdOrCreateData(systemUserInParam);
        UserCountTotalModel userCountTotalModel = new UserCountTotalModel();
        userCountTotalModel.setNowCount(nowCounter);

        nowCounter = systemUserService.selectSystemUserMaxByProjectIdOrCreateData(systemUserInParam);
        userCountTotalModel.setNowCountOld(nowCounter - userCountTotalModel.getNowCount());
        if (colSign)
        {
            ProjAvgAllInParam projAvgAllInParam = new ProjAvgAllInParam();
            List<String> list = new ArrayList<>(1);
            list.add(projectId);
            projAvgAllInParam.setProCode(list);
            projAvgAllInParam.setCurrentDate(createDate);
            final ProjAvgDto projAvgDto = projAvgService.selectProjAvgByProjCode(projAvgAllInParam);
            list.clear();
            if (projAvgDto == null) {
                userCountTotalModel.setSame(BigDecimal.valueOf(0));
            }
            else{
                userCountTotalModel.setSame(projAvgDto.getWeightNumSum());
            }
        }

        return ResultStruct.success(userCountTotalModel);
    }

    /**
     * 展示端方法：P2，方法ID：SE202107241738154828D141097159E
     *
     * @param productId  表report_systemUser,字段名projectCode:项目CODE
     * @param createDate 表report_systemUser,字段名createDate:所属年月
     * @return 用户同比率
     */
    @ApiOperation(value = "展示端方法：P2[用户同比率],方法ID：SE202107241738154828D141097159E", notes = "/systemUserController/V1/selectSystemUserSameCount?projectCode=projectCode&createDate=createDate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品CODE列表", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createDate", value = "所属年月", dataType = "Date", required = true)
    })
    @PostMapping("/V1/selectSystemUserSameCount")
    public ResultVO selectSystemUserSameCount(@RequestParam(value = "productId") String productId
            , @RequestParam(value = "createDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date createDate) {

        final List<String> list = projectAndProductService.selectProjectListByProductId(productId);
        if (list.isEmpty()) {
            return ResultStruct.success(0);
        }
        SystemUserP2InParam systemUserP2InParam = new SystemUserP2InParam();
        systemUserP2InParam.setProjectCode(list);
        systemUserP2InParam.setCreateDate(createDate);
        final int nowCount = systemUserService.selectSystemUserSameCount(systemUserP2InParam);
        final int withMonths = getPropertiesClass.getWithMonths();
        createDate = LhClass.getDevDate(createDate, withMonths);
        systemUserP2InParam.setCreateDate(createDate);
        final int oldCount = systemUserService.selectSystemUserSameCount(systemUserP2InParam);
        if (oldCount != 0)
            return ResultStruct.success(nowCount / oldCount * 100);
        else
            return ResultStruct.success(100);
    }


}